<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17.8.16
 * Time: 10:20
 */

namespace App\Http\Middleware;

use Illuminate\Support\Facades\Response;
use Closure;
use Illuminate\Support\Facades\Auth;

class AdminAuth
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard("admin")->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return Response::json([
                    'status_code' => 401,
                    'message'     => "未授权",
                ], 200, [], JSON_UNESCAPED_UNICODE);
            } else {
                return redirect()->guest("admin/login");
            }
        }

        return $next($request);
    }
}